package view;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import org.bushe.swing.event.EventBus;

import javax.swing.JPanel;

import constants.Constants;
import constants.Events;

/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
@SuppressWarnings("serial")
public class StartBox extends JPanel implements MouseListener{
	
	private static final int rectSide = Constants.GUI.squareSide;
	private Piece[] ps = new Piece[4];
	private String colorName = "";
	private Integer belongsTo;
	private Color c = null;
	private int pieces = 0;
	
	private int offsetX = 10;
	private int offsetY = 10;

	public StartBox(Color c, String colorName, int player) {
		belongsTo = player;
		this.setLayout(null);
		this.setSize(150, 120);
		this.setVisible(true);
		this.c = c;
		this.addMouseListener(this);
		this.colorName = colorName;
		repaint();

	}
	
	public void setColor(Color color){
		c = color;
	}
	
	public void specificAdd(Piece comp, int number){
		if(pieces < 4){
			Component c = super.add(comp);
			ps[number] = comp;
			pieces ++;

			switch(number){
			case 0: c.setBounds(25+offsetX, 10+offsetY, 50, 50); break;
			case 1: c.setBounds(80+offsetX, 10+offsetY, 50, 50); break;
			case 2: c.setBounds(25+offsetX, 65+offsetY, 50, 50); break;
			case 3: c.setBounds(80+offsetX, 65+offsetY, 50, 50); break;
			}
		}		
	}
	
	public void reset(){
		for(Piece p: ps){
			if(p != null){
				this.remove(p);
				p = null;
			}
		}
		pieces = 0;
	}

	@Deprecated
	public void addPiece(Piece comp){
		if(pieces < 4){
			Component c = super.add(comp);
			ps[pieces] = comp;
			pieces ++;

			switch(pieces){
			case 1: c.setBounds(25+offsetX, 10+offsetY, 50, 50); break;
			case 2: c.setBounds(80+offsetX, 10+offsetY, 50, 50); break;
			case 3: c.setBounds(25+offsetX, 65+offsetY, 50, 50); break;
			case 4: c.setBounds(80+offsetX, 65+offsetY, 50, 50); break;
			}
		}
	}
	
	@Deprecated
	public Piece popPiece(){
		System.out.println("Popping from sb "+pieces);
		if(pieces > 0){
			this.remove(ps[pieces-1]);
			Piece p = ps[pieces-1];
			p.setBounds(10, 10, 25, 25);
			ps[pieces-1] = null;
			pieces --;
			
			return p;
		}
		return null;
	}

	@Override
	public void paint(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;		
		g2.setColor(c);
		g2.fillRect(25, 10, rectSide, rectSide);
		g2.fillRect(80, 10, rectSide, rectSide);
		g2.fillRect(25, 65, rectSide, rectSide);
		g2.fillRect(80, 65, rectSide, rectSide);
		this.paintChildren(g);
	}	
	@Override
	public void mouseClicked(MouseEvent a) {
		int x = a.getX();
		int y = a.getY();

		if(x > 25 && x < 70 && y > 10 && y < 55){
			System.out.println(x+","+y+": Square TopLeft. Team: "+ colorName); 
				EventBus.publish(Events.MOVE.toString(), belongsTo == 1 ? 56: belongsTo == 2 ? 60: belongsTo == 3 ? 64: belongsTo == 4 ? 68: null);
		}
		else if(x > 80 && x < 125 && y > 10 && y < 55){
			System.out.println(x+","+y+": Square TopRight Team: "+ colorName);
				EventBus.publish(Events.MOVE.toString(), belongsTo == 1 ? 57: belongsTo == 2 ? 61: belongsTo == 3 ? 65: belongsTo == 4 ? 69: null);
		}
		else if(x > 25 && x < 70 && y > 65 && y < 110){
			System.out.println(x+","+y+": Square BL Team: "+ colorName);
				EventBus.publish(Events.MOVE.toString(), belongsTo == 1 ? 58: belongsTo == 2 ? 62: belongsTo == 3 ? 66: belongsTo == 4 ? 70: null);
		}
		else if(x > 80 && x < 125 && y > 65 && y < 110){
			System.out.println(x+","+y+": Square BR Team: "+ colorName);
				EventBus.publish(Events.MOVE.toString(), belongsTo == 1 ? 59: belongsTo == 2 ? 63: belongsTo == 3 ? 67: belongsTo == 4 ? 71: null);
		}
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {}
	
	@Override
	public void mouseExited(MouseEvent arg0) {}
	
	@Override
	public void mousePressed(MouseEvent arg0) {}
	
	@Override
	public void mouseReleased(MouseEvent arg0) {}
}